rule all:
    input:
        expand("{sample}.snv.vcf.gz", sample=config["sample"]),
        expand("{sample}.snv.vcf.gz.tbi", sample=config["sample"]),

rule normalize_vcf:
    input:
        raw_vcf="{sample}.raw.vcf.gz",
        ref_fasta=config["ref_fasta"],
    output:
        vaf_vcf=temp("{sample}.vaf.vcf.gz"),
        vaf_vcf_tbi=temp("{sample}.vaf.vcf.gz.tbi"),
        norm_vcf="{sample}.norm.vcf.gz",
        norm_vcf_tbi="{sample}.norm.vcf.gz.tbi",
    shell:
        """
        bcftools +fill-tags {input.raw_vcf} -Oz -o {output.vaf_vcf} -- -t VAF && \
        bcftools index --tbi {output.vaf_vcf} && \
        bcftools norm -f {input.ref_fasta} -m -any {output.vaf_vcf} | \
        awk '$1~/#/ || $5!~/[^ATGC]/' | bgzip -c > {output.norm_vcf} && \
        bcftools index --tbi {output.norm_vcf}
        """


if config.get("hard_filtering"):
    include: "rules/hard_filtering.smk"
else:
    include: "rules/vqsr.smk"